<template>
  <div>
    <el-row>
      <el-col :span="24">
        <!-- 放搜索条件 
        :inline="ture"让表单元素占一行,一行不够会换行-->
        <el-form :inline="true">
          <el-form-item label="咨询人电话">
            <el-input v-model="pdParam.tel" placeholder="请输入"></el-input>
          </el-form-item>
          <el-form-item label="接待时间">
            <el-date-picker
              v-model="pdParam.createTime"
              type="date"
              placeholder="选择日期"
              value-format="yyyy-MM-dd"
            ></el-date-picker>
          </el-form-item>

          <el-form-item>
            <el-button type="primary" @click="clear">重置</el-button>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="queryEmployee">查询</el-button>
          </el-form-item>
          <el-form-item>
          <el-button type="danger" @click="putawayVisible = true"
            >新增</el-button>
          </el-form-item>
        </el-form>
      </el-col>

      <el-col :span="24">
        <!-- 放表格数据 -->

        <el-table :data="tableData" style="width: 100%">
          <el-table-column prop="registerName" label="咨询人"></el-table-column>
          <el-table-column prop="tel" label="咨询人手机号"></el-table-column>
          <el-table-column prop="jdPerson" label="接待人员"></el-table-column>
          <el-table-column prop="perNum" label="接待人数"></el-table-column>
          <el-table-column prop="channelName" label="咨询途径"></el-table-column>
          <el-table-column prop="createTime" label="接待时间"></el-table-column>
          <el-table-column prop="labelName" label="标签"></el-table-column>
          <el-table-column prop="remark" label="备注"></el-table-column>
        </el-table>
      </el-col>
      <el-col :span="12">
        <!-- 放分页部分 -->
        <el-pagination
          background
          :page-sizes="[3, 5, 10, 20]"
          :page-size="pageSize"
          :current-page="pageNum"
          layout="total, sizes, prev, pager, next"
          @next-click="nextPage"
          @prev-click="prevPage"
          @size-change="sizeChange"
          @current-change="currentPage"
          :total="total"
        ></el-pagination>
      </el-col>
    </el-row>

    <el-dialog title="新增接待" :visible.sync="putawayVisible">
      <el-form :model="addParam" :rules="formRules" ref="addform">
        <el-form-item label="咨询人" label-width="100px" prop="registerName">
          <el-input v-model="addParam.registerName" placeholder="请输入咨询人姓名"></el-input>
        </el-form-item>
        <el-form-item label="咨询人电话" label-width="100px" prop="tel">
          <el-input v-model="addParam.tel" placeholder="请输入咨询人电话"></el-input>
        </el-form-item>
        <el-form-item label="接待客服" label-width="100px" prop="jdPersonId">
          <el-select v-model="addParam.jdPersonId" placeholder="请选择接待客服">
            <el-option
              v-for="person in jdPersons"
              :key="person.id"
              :label="person.name"
              :value="person.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="咨询途径" label-width="100px" prop="channelDictId">
          <el-select v-model="addParam.channelDictId" placeholder="请选择咨询途径">
            <el-option
              v-for="channel in channels"
              :key="channel.id"
              :label="channel.name"
              :value="channel.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="接待人数" label-width="100px" prop="perNum">
          <el-input-number v-model="addParam.perNum" :min="1" :max="10" :step="1"></el-input-number>
        </el-form-item>
        <el-form-item label="标签" label-width="100px" prop="labelId">
          <el-select v-model="addParam.labelId" placeholder="请选择标签">
            <el-option
              v-for="label in labels"
              :key="label.id"
              :label="label.name"
              :value="label.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" label-width="100px" prop="remark">
          <el-input v-model="addParam.remark"></el-input>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click="putawayVisible = false">取 消</el-button>
        <el-button type="primary" @click="addSubmit">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
  
  <script>
export default {
  data() {
    return {
      putawayVisible: false,
      pageNum: 1,
      pageSize: 10,
      total: 0,
      addParam: {
        registerName: "",
        tel: "",
        jdPersonId: "",
        perNum: "",
        channelDictId: "",
        labelId: "",
        remark: ""
      },
      pdParam: {
        tel: "",
        createTime: ""
      },
      tableData: [],
      jdPersons: [],
      channels: [],
      labels: [],
      formRules: {
        registerName: [
          { required: true, message: "请输入咨询人姓名", trigger: "blur" }
        ],
        tel: [{ required: true, message: "请输入咨询人电话", trigger: "blur" }],
        jdPersonId: [
          { required: true, message: "请选择接待客服", trigger: "change" }
        ],
        channelDictId: [
          { required: true, message: "请选择咨询途径", trigger: "change" }
        ],
        perNum: [
          { required: true, message: "请输入接待人数", trigger: "change" }
        ],
        labelId: [{ required: true, message: "请选择标签", trigger: "change" }],
        remark: []
      }
    };
  },
  methods: {
    addSubmit() {
      // 调用表单的验证方法
      this.$refs.addform.validate(valid => {
        if (valid) {
          // 表单验证通过后，执行保存操作
          this.$axios
            .post("api/register/addJd", this.addParam)
            .then(res => {
              if (res.data.code === 200) {
                this.putawayVisible = false;
                this.queryEmployee();
              } else {
                // 处理保存失败情况
                console.error(
                  "Error occurred while adding dictionary:",
                  res.data.msg
                );
                // 可以添加错误处理逻辑，比如提示用户保存失败
                this.$message.error("保存失败，请稍后重试");
              }
            })
            .catch(error => {
              // 处理保存失败情况
              console.error("Error occurred while adding dictionary:", error);
              // 可以添加错误处理逻辑，比如提示用户保存失败
              this.$message.error("保存失败，请稍后重试");
            });
        } else {
          // 如果表单验证未通过，可以给用户提示错误信息
          this.$message.error("表单数据验证失败，请检查输入");
        }
      });
    },

    clear() {
      this.pdParam = {};
    },
    prevPage(val) {
      //点击上一页触发
      this.pageNum = val;
      this.queryEmployee();
    },
    nextPage(val) {
      //点击下一页触发
      this.pageNum = val;
      this.queryEmployee();
    },
    sizeChange(val) {
      //val每一页条数
      this.pageNum = 1;
      this.pageSize = val;
      this.queryEmployee();
    },
    currentPage(val) {
      this.pageNum = val;
      this.queryEmployee();
    },
    queryCondition() {
      thispageNum = 1;
      this.queryEmployee();
    },
    queryEmployee() {
      //带分页参数pageNum,pageSize
      let pageParam = {};
      pageParam.pageNum = this.pageNum;
      pageParam.pageSize = this.pageSize;
      pageParam.param = this.pdParam;
      this.$axios.post("api/register/queryRRList", pageParam).then(res => {
        if (res.data.code == 200) {
          let pageData = res.data.data;
          this.tableData = pageData.list;
          //给分页参数赋值
          this.total = pageData.total;
          this.pageNum = pageData.pageNum;
          this.pageSize = pageData.pageSize;
        } else {
          this.$router.push({ path: "/login" });
        }
      });
    },
    queryjdPersons() {
      this.$axios.get("api/register/queryJd").then(res => {
        if (res.data.code == 200) {
          this.jdPersons = res.data.data;
          this.querychannels();
          this.querylabels();
        }
      });
    },
    querychannels() {
      this.$axios.get("api/dict/queryDict/" + 48).then(res => {
        if (res.data.code == 200) {
          this.channels = res.data.data;
        }
      });
    },
    querylabels() {
      this.$axios.get("api/dict/queryDict/" + 51).then(res => {
        if (res.data.code == 200) {
          this.labels = res.data.data;
        }
      });
    }
  },
  created() {
    this.queryjdPersons();
    this.queryEmployee();
  }
};
</script>
  
  <style scoped></style>